package com.one.consumer.user.presenter;

import android.content.Context;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.FieldCheckUtil;
import com.one.common.utils.Logger;
import com.one.common.utils.MyToast;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.base.IView;
import com.one.consumer.common.extra.LocationExtra;
import com.one.consumer.common.map.LocationUtils;
import com.one.consumer.user.model.UserModel;
import com.one.consumer.user.model.extra.AddressExtra;

import java.util.HashMap;
import java.util.Map;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/28 5:34 PM
 * description ：添加或者修改地址
 **/
public class AddAndUpdateAddressPresenter extends BaseApiPresenter<IView, UserModel> {
    private AddressExtra mExtra;
    private Map<String, String> params = new HashMap<>();
    public AddAndUpdateAddressPresenter(IView mView, Context mContext) {
        super(mView, mContext, new UserModel((BaseActivity) mContext));
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mExtra = (AddressExtra) mActivity.getIntent().getSerializableExtra(AddressExtra.getExtraName());
    }

    /**
     * 提交
     */
    public void submit(String contact_name, String contact_mobile, String address_detail, LocationExtra extra){
        if(mExtra!=null){
            // 添加地址
            if(mExtra.getType() == AddressExtra.ADD){
                addAddress(contact_name, contact_mobile, address_detail, extra);
            } else {
                //更新地址
                params.put("address_id",mExtra.getAddressItem().getAddress_id()+"");
                addAddress(contact_name, contact_mobile, address_detail, extra);
            }
        }
    }

    /**
     * 添加
     */
    private void addAddress(String contact_name, String contact_mobile, String address_detail, LocationExtra extra){
        if (StringUtils.isBlank(contact_name)) {
            Toaster.showLongToast("请输入联系人", MyToast.FAIL);
            return;
        } else if (StringUtils.isBlank(contact_mobile)) {
            Toaster.showLongToast("请输入联系人手机号", MyToast.FAIL);
            return;
        } else if (!FieldCheckUtil.checkPhoneNumber(contact_mobile)) {
            Toaster.showLongToast("请输入正确的联系人手机号", MyToast.FAIL);
            return;
        } else if (extra == null) {
            Toaster.showLongToast("请选择地址", MyToast.FAIL);
            return;
        } else if (StringUtils.isBlank(address_detail)) {
            Toaster.showLongToast("请输入详细地址", MyToast.FAIL);
            return;
        }

        // 获取详细地址
        LocationUtils.reGeocodeSearch(mActivity,
                new LatLonPoint(extra.getLatitude(), extra.getLongitude()),
                result -> {
                    params.put("address_province", result.getProvince());
                    params.put("address_city", result.getCity());
                    params.put("address_country", result.getDistrict());

                    params.put("address_name", contact_name);
                    params.put("address_mobile", contact_mobile);
                    params.put("address_detailed", extra.getTitle() + address_detail);
                    params.put("address_longitude", extra.getLongitude()+"");
                    params.put("address_latitude", extra.getLatitude()+"");

                    mModel.addAndUpdateAddress(params, new ObserverOnResultListener<Object>() {
                        @Override
                        public void onResult(Object o) {
                            if(mView!=null){
                                Toaster.showLongToast("添加地址成功！",MyToast.SUCCESS);
                                mView.finishPage();
                            }
                        }
                    });
                });
    }

}
